<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="110px"
    >
      <!-- 微信openId -->
      <el-form-item label="微信openId" prop="wxOpenId">
        <el-input v-model="dataForm.wxOpenId" placeholder="微信openId"></el-input>
      </el-form-item>

      <!-- 客户昵称 -->
      <el-form-item label="客户昵称" prop="nickname">
        <el-input v-model="dataForm.nickname" placeholder="客户昵称"></el-input>
      </el-form-item>

      <!-- 性别 -->
      <el-form-item label="性别" prop="gender">
        <el-select
          v-model="dataForm.gender"
          placeholder="请选择性别"
          style="width: 100%;"
        >
          <el-option label="男" :value="1"></el-option>
          <el-option label="女" :value="2"></el-option>
        </el-select>
      </el-form-item>

      <!-- 头像 -->
      <el-form-item label="头像" prop="avatarUrl">
        <oss-upload  @upload-success="handleUploadSuccess" />
      </el-form-item>

      <!-- 电话 -->
      <el-form-item label="电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="电话"></el-input>
      </el-form-item>

      <!-- 状态 -->
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="dataForm.status"
          placeholder="请选择状态"
          style="width: 100%;"
        >
          <el-option label="有效" :value="1"></el-option>
          <el-option label="禁用" :value="2"></el-option>
        </el-select>
      </el-form-item>

      <!-- 创建时间 -->
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="dataForm.createTime"
          type="datetime"
          placeholder="选择日期时间"
          style="width: 100%;"
          :clearable="true"
          
        ></el-date-picker>
      </el-form-item>

      <!-- 修改时间 -->
      <el-form-item label="修改时间" prop="updateTime">
        <el-date-picker
          v-model="dataForm.updateTime"
          type="datetime"
          placeholder="选择日期时间"
          style="width: 100%;"
        ></el-date-picker>
      </el-form-item>

      <!-- 是否删除 -->
      <el-form-item label="是否删除" prop="isDeleted">
        <el-switch
          v-model="dataForm.isDeleted"
          :active-value="1"
          :inactive-value="0"
          :disabled="true"
        ></el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import OssUpload from '@/utils/oss-upload.vue';
import {timeMap as formatDate,validatePhone} from "@/utils";
import http from "@/utils/request";
import {Message} from "element-ui";

export default {
  components: {
    OssUpload,
  },
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        wxOpenId: "",
        nickname: "",
        gender: "",
        avatarUrl: "",
        phone: "",
        status: "", // 状态，默认为空
        createTime: "",
        updateTime: "",
        isDeleted: 0, // 默认未删除
      },
      dataRule: {
        wxOpenId: [
          { required: true, message: "微信openId不能为空", trigger: "blur" },
        ],
        nickname: [
          { required: true, message: "客户昵称不能为空", trigger: "blur" },
        ],
        gender: [{ required: true, message: "性别不能为空", trigger: "blur" }],
        phone: [{ required: true, message: "电话不能为空", trigger: "blur" }],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateTime: [{ required: true, message: "不能为空", trigger: "blur" }],
        isDeleted: [
          { required: true, message: "是否删除不能为空", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    handleUploadSuccess(url)
    {
      alert("接受到的url")
  console.log("接收到的这个oss传递过来的url地址",url);
      this.avatarUrl =url
      //  最后把这个数据进行这个添加到这个地址当中
    },
    mounted() {
      this.nickname='';
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;

      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/customer/customerinfo/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.dataForm.wxOpenId = data.customerInfo.wxOpenId;
              this.dataForm.nickname = data.customerInfo.nickname;
              this.dataForm.gender = data.customerInfo.gender;
              this.dataForm.avatarUrl = data.customerInfo.avatarUrl;
              this.dataForm.phone = data.customerInfo.phone;
              this.dataForm.status = data.customerInfo.status; // 状态值
              this.dataForm.createTime = formatDate.formatDate(data.customerInfo.createTime);
              this.dataForm.updateTime = formatDate.formatDate(data.customerInfo.updateTime);
              this.dataForm.isDeleted = data.customerInfo.isDeleted; // 是否删除

            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
       this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          if (!validatePhone(this.dataForm.phone)) {
            Message.error('电话号码输入不正确')
            return
          }
          http({
            url:
              `/customer/customerinfo/${!this.dataForm.id ? "save" : "update"}`,
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              wxOpenId: this.dataForm.wxOpenId,
              nickname: this.dataForm.nickname,
              gender: this.dataForm.gender,
              avatarUrl: this.dataForm.avatarUrl,
              phone: this.dataForm.phone,
              status: this.dataForm.status, // 状态值
              createTime: formatDate.formatDate(this.dataForm.createTime),
              updateTime:formatDate.formatDate(this.dataForm.updateTime),
              isDeleted: this.dataForm.isDeleted, // 是否删除
            }),
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>